/**
 * Created By xiyuan
 * Author server@xiyuan.name (惜缘叛逆)
 * DateTime 2019/3/30 11:53 AM
 * Describe javascript功能描述
 * MIT License http://www.opensource.org/licenses/mit-license.php
 */

// 文件工具
const fs = require('fs');
// 路径包
const path = require('path');

// 确保目录存在
function dirExists(dir) {
	try {
		let stats = fs.statSync(dir);
		if (!stats.isDirectory()) throw new Error("")
	}
	catch (e) {
		let _dir = path.parse(dir).dir;
		dirExists(_dir)
		fs.mkdirSync(dir);
	}
	return dir;
}


function getType(value) {
	let type = typeof (value);
	if (type == 'object') {
		type = Object.prototype.toString.call(value).match(/Object\s+([a-z0-9_]*)/i)[1].toLocaleLowerCase()
	}
	return type;
}

let date = {},
	
	//获取当前时间戳
	nowTimestamp = date.nowTimestamp = function () {
		return new Date().getTime();
	},
	
	/*时间转换*/
	convert = date.convert = function () {
		let date = arguments[0];
		if ((date = Number(date) ? Number(date) : date)) {
			if (typeof date === 'string') {
				date = new Date(date.replace(/-/g, "/"));
			} else {
				date = new Date(date);
			}
			date = date.toString().indexOf('Invalid') >= 0 ? new Date() : date;
		} else if ({}.toString.call(date) === '[object Date]') {
		} else {
			date = new Date();
		}
		
		if (typeof arguments[1] === "string") {
			date = getJavaTime(date, arguments[1])
		}
		return date;
	},
	
	//获取年份
	getYear = date.getYear = date.getFullYear = function (data) {
		return convert(data).getFullYear();
	},
	
	//获取月份
	getMonth = date.getMonth = function (data) {
		return convert(data).getMonth() + 1
	},
	
	//获取日
	getDate = date.getDate = function (data) {
		return convert(data).getDate();
	},
	
	//获取时
	getHours = date.getHours = function (data) {
		return convert(data).getHours();
	},
	
	//获取分
	getMinutes = date.getMinutes = function (data) {
		return convert(data).getMinutes();
	},
	
	//获取秒
	getSeconds = date.getSeconds = function (data) {
		return convert(data).getSeconds();
	},
	
	/*java时间戳转换*/
	getJavaTime = date.getJavaTime = function (data, layout) {
		let time = convert(data);
		let year = time.getFullYear()
		let month = time.getMonth() + 1
		let date = time.getDate()
		let hours = time.getHours()
		let minutes = time.getMinutes() >= 10 ? time.getMinutes() : time.getMinutes();
		let seconds = time.getSeconds()
		if (typeof(layout) !== "string") {
			layout = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
		} else {
			layout = layout.replace(/yy/i, year)
			layout = layout.replace(/y/i, String(year).slice(-2))
			layout = layout.replace(/mm/i, month > 9 ? month : '0' + month)
			layout = layout.replace(/m/i, month)
			layout = layout.replace(/dd/i, date > 9 ? date : '0' + date)
			layout = layout.replace(/d/i, date)
			layout = layout.replace(/hh/i, hours > 9 ? hours : '0' + hours)
			layout = layout.replace(/h/i, hours)
			layout = layout.replace(/ii/i, minutes > 9 ? minutes : '0' + minutes)
			layout = layout.replace(/i/i, minutes)
			layout = layout.replace(/ss/i, seconds > 9 ? seconds : '0' + seconds)
			layout = layout.replace(/s/i, seconds)
		}
		return layout;
	},
	
	/*PHP时间戳转换*/
	getPhpTime = date.getPhpTime = function (nS, layout) {
		return date.getJavaTime(nS * 1000, layout);
	},
	
	/*添加秒*/
	addDateMinutes = date.addDateMinutes = function (date, minutes) {
		date = convert(date);
		date = new Date(date.getTime());
		date.setMinutes(date.getMinutes() + minutes);
		return date;
	};

/*添加天数*/
date.addDate = function (date, days) {
	date = new Date(convert(date));
	date.setDate(date.getDate() + days)
	return date;
}
/*添加分钟*/
date.addMinutes = function (date, days) {
	date = new Date(convert(date));
	date.setMinutes(date.getMinutes() + days)
	return date;
}


/*获取当前月份有多少天*/
date.getMonthCountDate = function (date) {
	date = date ? (getType(date) === 'date' ? date : new Date(Number(date))) : new Date();
	date.setMonth(date.getMonth() + 1);
	date.setDate(0);
	return date.getDate();
};


/*获取第几周*/
date.getNowWeek = function (nowDate) {
	nowDate = convert(nowDate);
	let startDate = new Date(nowDate.getTime());
	startDate.setMonth(0);
	startDate.setDate(1);
	startDate.setHours(0, 0, 0, 0);
	
	let countDay = (nowDate.getTime() - startDate.getTime()) / 1000 / 60 / 60 / 24 + 1,
		tmpDay = countDay - (8 - startDate.getDay());
	
	return (tmpDay > 0 ? Math.ceil(tmpDay / 7) : 0) + 1;
};


module.exports = {
	dirExists,
	date,
	getType,
	// 创建日志对象
	createLog(err, type) {
		const stack = err.stack.match(/\(([^\)]+)\)/) || ['', __filename];
		return {
			date: date.convert(Date.now(), 'yy/mm/dd hh:ii:ss'),
			type: type || 'E',
			codeFile: path.parse(stack[1]).base,
			msg: err.message,
		}
	}
}